$(function () {
  function readData() {
    var list = localStorage.getItem("users");
    if (list !== null) {
      return JSON.parse(list);
    } else {
      return [];
    }
  }

  function savaData(data) {
    localStorage.setItem("users", data);
  }

  function success(ele, msg) {
    ele.siblings("span").prop("class", "success");
    ele.siblings("span").children("i").prop("class", "success-icon");
    ele.siblings("span").children("p").text(msg);
  }

  function error(ele, msg) {
    ele.siblings("span").prop("class", "error");
    ele.siblings("span").children("i").prop("class", "error-icon");
    ele.siblings("span").children("p").text(msg);
  }

  function clear(ele) {
    ele.siblings("span").prop("class", "");
    ele.siblings("span").children("i").prop("class", "");
    ele.siblings("span").children("p").text("");
  }

  var users = readData();
  var newUser = {
    username: "",
    phone: "",
    password: "",
    remember: false,
  };
  var pass = true;
  const regNum = /^[0-9]*$/;
  const regChar = /^[0-9A-Za-z]+$/;
  const regSpe = /^[`!,.?]+$/;
  console.log(regChar.test("123a"));

  $("#phone").blur(function () {
    if ($(this).val().length == 11) {
      success($(this), "正确");
      newUser.phone = $(this).val();
      pass = true;
    } else {
      error($(this), "格式错误，请重新输入");
      pass = false;
    }
  });

  $("#username").blur(function () {
    var username = $(this).val();
    console.log(username);
    if (username != "") {
      var repeat = false;
      for (var i = 0; i < users.length; i++) {
        if (username == users[i].username) {
          error($(this), "用户名重复，请重新输入");
          repeat = true;
          pass = false;
        }
      }
      if (!repeat) {
        success($(this), "正确");
        username = username.replace(/^\s*|\s*$/g, "");
        newUser.username = username;
        pass = true;
      }
    } else {
      error($(this), "请输入用户名");
      pass = false;
    }
  });
  $("#password").blur(function () {
    var password = $(this).val();
    if (password.length < 6) {
      error($(this), "密码长度过短，请重新输入");
      $(".low").fadeOut();
      $(".middle").fadeOut();
      $(".strong").fadeOut();
      pass = false;
    } else {
      var low = true;
      if (regNum.test(password)) {
        $(".low").fadeIn();
        $(".middle").fadeOut();
        $(".strong").fadeOut();
      } else {
        if (regChar.test(password)) {
          $(".low").fadeIn();
          $(".middle").fadeIn();
          $(".strong").fadeOut();
          low = false;
        } else {
          $(".low").fadeIn();
          $(".middle").fadeIn();
          $(".strong").fadeIn();
          low = false;
        }
      }
      if (low) {
        error($(this), "密码等级过低，请重新输入");
        pass = false;
      } else {
        success($(this), "正确");
        pass = true;
      }
    }
    if (pass) {
      newUser.password = password;
    }
  });

  $("#apassword").blur(function () {
    var again = $(this).val();
    if (again != newUser.password) {
      error($(this), "密码不一致，请重新输入");
      pass = false;
    } else {
      success($(this), "正确");
      pass = true;
    }
  });

  $(".confirm").click(function () {
    var checked = $("#agree").prop("checked");
    if (checked) {
      clear($("#agree"));
      if (pass) {
        users.push(newUser);
        savaData(JSON.stringify(users));
        document.location.href = "register-to.html";
      }
    } else {
      error($("#agree"), "请勾选");
    }
  });
});
